package com.zhang.admin.service;

import com.zhang.admin.bean.Permission;
import com.zhang.admin.util.PermissionHelper;

import javax.swing.plaf.SliderUI;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @title: 权限服务
 * @author: zhangyf
 * @date: 2023/3/16 14:02
 **/
public class PermissionService {

    /**
     * 获取所有权限信息
     * @return
     */
    public static List<Permission> getAllPermission(){
        try {
            Class.forName(DATABASE.DRIVER);
            var connection = DriverManager.getConnection(
                    DATABASE.URL,
                    DATABASE.USERNAME,
                    DATABASE.PASSWORD
            );
            var selectStatement = connection.prepareStatement(
                    "select id,parent_id,permission_name,permission_code from permission"
            );
            var resultSet = selectStatement.executeQuery();
            var permissions = new ArrayList<Permission>();
            while (resultSet.next()){
                var permission = new Permission();
                permission.setId(resultSet.getLong("id"));
                permission.setParentId(resultSet.getLong("parent_id"));
                permission.setPermissionName(resultSet.getString("permission_name"));
                permission.setPermissionCode(resultSet.getString("permission_code"));
                permissions.add(permission);
            }

            // 根据获取的所有的权限构建权限结构
            permissions = PermissionHelper.build(permissions);

            selectStatement.close();
            connection.close();
            return permissions;
        }catch (SQLException | ClassNotFoundException e){
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 根据userId获取对应user的权限列表
     * @param userId
     * @return
     */
    public static List<String> getPermissionById(Long userId){
        try {
            Class.forName(DATABASE.DRIVER);
            var connection = DriverManager.getConnection(
                    DATABASE.URL,
                    DATABASE.USERNAME,
                    DATABASE.PASSWORD
            );
            var selectStatement = connection.prepareStatement(
                    "select permission_code as permissionCode from user " +
                            "inner join user_role " +
                            "inner join role_permission " +
                            "inner join permission " +
                            "on user.id = user_role.user_id " +
                            "and user_role.role_id = role_permission.role_id " +
                            "and role_permission.permission_id = permission.id " +
                            "where user.id = ? "
            );
            selectStatement.setLong(1,userId);
            var resultSet = selectStatement.executeQuery();
            var permissions = new ArrayList<String>();
            while (resultSet.next()){
                permissions.add(resultSet.getString("permissionCode"));
            }

            selectStatement.close();
            connection.close();

            return permissions;
        }catch (SQLException | ClassNotFoundException e){
            e.printStackTrace();
        }
        return new ArrayList<>();
    }


    public static void main(String[] args) {
        System.out.println(PermissionService.getPermissionById(3L));
    }
}
